package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.annotation.Log;
import com.itheima.dao.StudentMapper;
import com.itheima.dto.StudentAndClazzDTO;
import com.itheima.po.*;
import com.itheima.pojo.Result;
import com.itheima.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    StudentMapper studentMapper;

    @Override
    public Result selectStudent(StudentPageParam param) {

        PageHelper.startPage(param.getPage(), param.getPageSize());
        Page<StudentAndClazzDTO> students = studentMapper.selectStudent(param);
        PageVO<StudentAndClazzDTO> pageVO = new PageVO<>();
        pageVO.setTotal(students.getTotal());
        pageVO.setRows(students.getResult());
        return Result.success(pageVO);
    }

    @Override
    @Log
    public Result insertStudent(InsertStudent student) {
        IsExists isExists = studentMapper.selectIsExists(student.getNo());
        if (isExists != null) {
            return Result.fail("学号已存在");
        }
        int i = studentMapper.insertStudent(student);
        return Result.success("成功插入" + i + "条数据");
    }

    @Override
    @Log
    public Result deleteStudentByIds(List<Integer> ids) {
        if(ids.isEmpty()){
            return Result.fail("请选择要删除的记录");
        }
        int i = studentMapper.deleteByIds(ids);
        return Result.success("成功删除" + i + "条数据");
    }

    @Override
    public Result selectStudentById(Integer id) {
        SelectStudentById student = studentMapper.selectStudentById(id);
        return Result.success(student);
    }

    @Override
    @Log
    public Result updateStudent(UpdateStudent student) {
        IsExists isExists = studentMapper.selectIsExists(student.getNo());
        if (isExists != null && !isExists.getId().equals(student.getId())) {
            return Result.fail("学号已存在");
        }
        int i = studentMapper.updateStudent(student);
        return Result.success("成功更新" + i + "条数据");
    }

    @Override
    @Log
    public Result violation(Integer id, Integer score) {
        int violation = studentMapper.violation(id, score);
        return Result.success("成功违纪处理" + violation + "个员工");
    }

}
